package org.jetnuke.modules.calendar;


import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.CalendarEvent;
import org.jetnuke.servlets.utilities.StringUtils;
import org.jetnuke.util.JetUtil;

public class DoAddEvent implements IAction {
	String title;
	String fullDesc="TODO";
	int evtType;
	int startMonth;
	int startDay;
	int startYear;
	int tradio;
	int startHour;
	int startMin;
	int durHour;
	int durMin;
	String location;
	int rept;
	int freq;
	int wf;
	int wd;
	int wx;
	int exp;
	int endMonth;
	int endDay;
	int endYear;
	
	public void setDurMin(int durMin) {
		this.durMin = durMin;
	}
	public void setEndDay(int endDay) {
		this.endDay = endDay;
	}
	public void setEndMonth(int endMonth) {
		this.endMonth = endMonth;
	}
	public void setEndYear(int endYear) {
		this.endYear = endYear;
	}
	public void setEvtType(int evtType) {
		this.evtType = evtType;
	}
	public void setExp(int exp) {
		this.exp = exp;
	}
	public void setFreq(int freq) {
		this.freq = freq;
	}
	public void setFullDesc(String fullDesc) {
		this.fullDesc = StringUtils.filterText(fullDesc,false);
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public void setRept(int rept) {
		this.rept = rept;
	}
	public void setStartDay(int startDay) {
		this.startDay = startDay;
	}
	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}
	public void setStartMin(int startMin) {
		this.startMin = startMin;
	}
	public void setStartMonth(int startMonth) {
		this.startMonth = startMonth;
	}
	public void setStartYear(int startYear) {
		this.startYear = startYear;
	}
	public void setTitle(String title) {
		this.title = StringUtils.filterText(title,true);
	}
	public void setTradio(int tradio) {
		this.tradio = tradio;
	}
	public void setWd(int wd) {
		this.wd = wd;
	}
	public void setWf(int wf) {
		this.wf = wf;
	}
	public void setWx(int wx) {
		this.wx = wx;
	}
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if(ui==null || ui.getID()<2)
			return null;
		CalendarEvent ce=new CalendarEvent();
		ce.setTITLE(title);
		ce.setFULLDESC(fullDesc);
		ce.setUID(ui.getID());
		ce.setLOCATION(location);
		ce.setRECURRING(rept);
		ce.setTYPE(evtType);
		GregorianCalendar start_date=new GregorianCalendar();
		start_date.set(Calendar.YEAR,startYear);
		start_date.set(Calendar.MONTH,startMonth);
		start_date.set(Calendar.DAY_OF_MONTH,startDay);
		ce.setSTART_DATE(start_date.getTime());
		if(tradio==1){
			GregorianCalendar start_time=new GregorianCalendar();
			start_time.set(Calendar.HOUR_OF_DAY,startHour);
			start_time.set(Calendar.MINUTE,startMin);
			start_time.set(Calendar.SECOND,0);
			ce.setSTART_TIME(start_time.getTime());
			GregorianCalendar duration=new GregorianCalendar();
			duration.set(Calendar.HOUR_OF_DAY,durHour);
			duration.set(Calendar.MINUTE,durMin);
			duration.set(Calendar.SECOND,0);
			ce.setDURATION(duration.getTime());
			
		}
		if(rept==1){//Repeat every
			ce.setFREQ(freq);//day, week, ...
		}else if(rept==2){//Repeat on the 
			ce.setWF(wf);//First, Second, Third, Fourth, Last
			ce.setWD(wd);//Sunday, Monday, ....
			ce.setWX(wx);//Every month, other month, 3 month,....
		}
		if(rept>0 && exp>0){
			GregorianCalendar expirationDate=new GregorianCalendar(endYear,endMonth, endDay);
			ce.setEXPIRATION(expirationDate.getTime());
		}
		DataAccessDriver.getInst().newCalendarEventDAO().addNew(ce);
		JetUtil.redirect(vc,response,request.getContextPath()+"/do/calendar");
		return null;
	}
	public void setDurHour(int durHour) {
		this.durHour = durHour;
	}

}
